package com.nowcoder.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nowcoder.entity.Message;
import com.nowcoder.mapper.MessageMapper;
import com.nowcoder.service.MessageService;
import com.nowcoder.utils.SensitiveFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.util.HtmlUtils;

import java.util.List;

/**
 * @Author liujilong
 * @Project nowcoder
 * @File CommentServiceImpl
 * @Date 2022/1/17 5:31 下午
 **/
@Service
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> implements MessageService {

    @Autowired
    private SensitiveFilter sensitiveFilter;

    @Override
    public List<Message> selectConversations(String uid, int offset, int limit) {
        return baseMapper.selectConversations(uid, offset, limit);
    }

    @Override
    public int selectConversationCount(String uid) {
        return baseMapper.selectConversationCount(uid);
    }

    @Override
    public List<Message> selectLetters(String conversationId, int offset, int limit) {
        return baseMapper.selectLetters(conversationId, offset, limit);
    }

    @Override
    public int selectLetterCount(String conversationId) {
        return baseMapper.selectLetterCount(conversationId);
    }

    @Override
    public int selectLetterUnreadCount(String uid, String conversationId) {
        return baseMapper.selectLetterUnreadCount(uid, conversationId);
    }

    @Override
    public int insertMessage(Message message) {
        message.setContent(HtmlUtils.htmlEscape(message.getContent()));
        message.setContent(sensitiveFilter.filter(message.getContent()));

        return baseMapper.insert(message);
    }

    @Override
    public int updateStatus(List<Integer> ids, int status) {
        return baseMapper.update(null, new UpdateWrapper<Message>().in("id", ids).set("status", status));
    }
}
